function hideDiv(div_id) {
	$("#" + div_id).animate({
		opacity : "hide"
	}, 300);
}
function popupDiv(div_id) {
	var div_obj = $("#" + div_id);
	// 窗口宽度,高度
	var winWidth = $(window).width();
	var winHeight = $(window).height();
	// 弹出的div的宽度,高度
	var popHeight = div_obj.height();
	var popWidth = div_obj.width();
	div_obj.animate({
		opacity : "show",
		left : (winWidth - popWidth) / 2,
		top : (winHeight - popHeight) / 2,
		width : popWidth,
		height : popHeight
	}, 300);
}

$(function() {
	
	$(".pop_box").easydrag();
	$(".pop_box").setHandler(".pop_box .p_head");
});
$(function() {
	$("#AddEmp_div").easydrag();
	$("#AddEmp_div").setHandler("#AddEmp_div #AddEmp_head");
});

$(function() {
	
	$("#UpdateEmp_div").easydrag();
	$("#UpdateEmp_div").setHandler("#UpdateEmp_div #UpdateEmp_head");
});


function padd()
{
	if(!RolePowerManager())
	{
		return;
	}
	popupDiv('AddEmp_div');
}

$(function() {
	
	
	EducationManager.getAll(function(data) {
		for ( var i = 0; i < data.length; i++) {
			$("#educationtype").append(
					"<option value='" + data[i].id + "'>" + data[i].name
					+ "</option>");
		}
	});
	
	EduDegreeManager.getAll(function(data) {
		for ( var i = 0; i < data.length; i++) {
			$("#eduDegreetype").append(
					"<option value='" + data[i].id + "'>" + data[i].name
					+ "</option>");
		}
	});
	
	dwrDeptManager.getALLDept(function(data) {
	
		for ( var i = 0; i < data.length; i++) {
			$("#depttype").append(
					"<option value='" + data[i].id + "'>"
					+ data[i].name + "</option>");
		}
	});
	
	DutyManager.getAllDuty(function(data) {
		
		for ( var i = 0; i < data.length; i++) {
			$("#dutytype").append(
					"<option value='" + data[i].id + "'>" + data[i].name
					+ "</option>");
		}
	});
	
	SkillGradeManager.getAll(function(data) {
	
		for ( var i = 0; i < data.length; i++) {
			$("#skilltype").append(
					"<option value='" + data[i].id + "'>" + data[i].name
					+ "</option>");
		}
	});
	
	EmpStatusManager.getAllEmpStatus(function(data) {
		for ( var i = 0; i < data.length; i++) {
			$("#empstatustype").append(
					"<option value='" + data[i].id + "'>" + data[i].name
					+ "</option>");
		}
	});
	
});

function clickEditEmp(count, id, div_id) {
	var counts = [];
	for ( var i = 1; i <= 5; i++) {
		counts[i] = '#' + count + i;
	}

	
	$("#name_edit").val($(counts[1]).text());
	$("#empno_edit").val($(counts[2]).text());
	$("#sex_edit").val($(counts[3]).text());
	$("#photo_edit").val($(counts[4]).text());
	$("#idCardid_edit").val($(counts[5]).text());
	$("#educationimg_edit").val($(counts[6]).text());
	$("#salary_edit").val($(counts[7]).text());
	$("#birthday_edit").val($(counts[8]).text());
	$("#address_edit").val($(counts[9]).text());
	$("#phone_edit").val($(counts[10]).text());
	$("#education_edit").val($(counts[11]).text());
	$("#eduDegree_edit").val($(counts[12]).text());
	$("#duty_edit").val($(counts[13]).text());
	$("#jobTitle_edit").val($(counts[14]).text());
	$("#empStatus_edit").val($(counts[15]).text());
	$("#skillGrade_edit").val($(counts[16]).text());
	$("#dept_edit").val($(counts[17]).text());
	$("#empid").val($(counts[18]).text());

	$("#dossierid").val(id);
	popupDiv(div_id);
}





function addEmp() {

	alert(1);
	
	var name= $("#name_add").val();
	var empno= $("#empno_add").val();
	var sex= $("#sex_add").val();
	var photo= $("#photo_add").val();
	var idCardid= $("#idCardid_add").val();
	var educationimg= $("#educationimg_add").val();
	var salary= $("#salary_add").val();
	var birthday= $("#birthday_add").val();
	var address= $("#address_add").val();
	var phone= $("#phone_add").val();
	var education= $("#education_add").val();
	var eduDegree= $("#eduDegree_add").val();
	var duty= $("#duty_add").val();
	var jobTitle= $("#jobTitle_add").val();
	var empStatus= $("#empStatus_add").val();
	var skillGrade= $("#skillGrade_add").val();
	var dept= $("#dept_add").val();
	
	if (name=="" || empno=="" || sex=="" || address=="" ||phone=="" ||education=="" ||eduDegree=="" ||jobTitle=="" ||birthday=="" ||salary=="" ||idCardid=="" ||
			 duty=="" ||empStatus=="" ||skillGrade=="" ||dept=="" ||educationimg=="" ||photo=="" ) {
		alert("有必填项未填！");
		return; }
		EmpManager.addEmp(name , empno , sex , photo , idCardid , educationimg , salary , birthday , address ,
				phone , education , eduDegree , duty , jobTitle , empStatus , skillGrade , dept, function(data) {
					if (data) {
						alert("增加成功");
						location.reload("/AddEmpServlet");
					} else {
						alert("增加失败");
					}
				});
	
}

function updateEmp() {
	
	var name= $("#name_edit").val();
	var empno= $("#empno_edit").val();
	var sex= $("#sex_edit").val();
	var photo= $("#photo_edit").val();
	var idCardid= $("#idCardid_edit").val();
	var educationimg= $("#educationimg_edit").val();
	var salary= $("#salary_edit").val();
	var birthday= $("#birthday_edit").val();
	var address= $("#address_edit").val();
	var phone= $("#phone_edit").val();
	var education= $("#education_edit").val();
	var eduDegree= $("#eduDegree_edit").val();
	var duty= $("#duty_edit").val();
	var jobTitle= $("#jobTitle_edit").val();
	var empStatus= $("#empStatus_edit").val();
	var skillGrade= $("#skillGrade_edit").val();
	var dept= $("#dept_edit").val();
	var id = $("#empid").val();	
	
	
	
	




	if (name=="" || empno=="" || sex=="" || address=="" ||phone=="" ||education=="" ||eduDegree=="" ||jobTitle=="" ||birthday=="" ||salary=="" ||idCardid=="" ||
			 duty=="" ||empStatus=="" ||skillGrade=="" ||dept=="" ||educationimg=="" ||photo=="" ) {
		alert("有必填项未填！");
		return; }
		EmpManager.updateEmp(id, name , empno , sex , photo , idCardid , educationimg , salary , birthday , address ,
				phone , education , eduDegree , duty , jobTitle , empStatus , skillGrade , dept , function(data) {
					if (data) {
						alert("修改成功");
						location.reload("/GetAllEmpServlet");
					} else {
						alert("修改失败");
					}
				});
}
//关闭窗口顺带刷行，避免多行数据的问题出现
function closediv()
{
	hideDiv('UpdateEmp_div');
	location.reload();
}

	//更换每页显示页数
	function changePageSize()
	{
			var pageSize=$("#pageSize").val();
			location.href="FindEmpServlet?pageSize="+pageSize;
	}
	
	function search() {
		if ($("#searchtype").val() == "age" || $("#searchtype").val() == "empid") {

			if (isNaN($("#keycode").val())) {
				alert("请输入数字");
				return;
			}
		}
		$("#searchform").submit();
	}
	
	
	function changesearchtype()
	{
		
		
		var type=$("#serchtype").val();
		
		if("time"==type)
		{
			$("#searchtypeoption").animate({opacity : "hide"}, 500);
			$("#searchtinput").animate({opacity : "hide"}, 500);
		}
	else if("trainname"==type || "empname"==type||"empid"==type || "sex"==type)
		{		
		$("#searchtypeoption").animate({opacity : "hide"}, 500);
		$("#searchtinput").animate({opacity : "show"}, 500);
		}else
		{
			$("#searchtypeoption").animate({opacity : "show"}, 500);
			$("#searchtinput").animate({opacity : "hide"}, 500);	
		}
		//如果是按类型查询
		
		if("duty"==type)
		{
		$("#idkeycode").empty();
		DutyManager.getAllDuty(function(data)
					{
						for(var i=0;i<data.length;i++)
							{
								$("#idkeycode").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>");
							}
					});
		}
		if("education"==type)
		{
		$("#idkeycode").empty();
		EducationManager.getAll(function(data)
					{
						for(var i=0;i<data.length;i++)
							{
								$("#idkeycode").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>");
							}
					});
		f("status"==type)
		{
		$("#idkeycode").empty();
		EmpStatusManager.getAll(function(data)
					{
						for(var i=0;i<data.length;i++)
							{
								$("#idkeycode").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>");
							}
					});
		}
	}

function deleteEmp(id) {
	if(!RolePowerManager())
	{
		return;
	}
	EmpManager.deleteById(id, function(data) {
		if (data) {
			alert("修改成功");
			location.reload("/GetAllEmpServlet");
		} else {
			alert("修改失败");
		}
	});
}
}